{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d5299fad",
   "metadata": {},
   "source": [
    "# numpy的基本用法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ed1af1c",
   "metadata": {},
   "source": [
    "## numpy有什么特点\n",
    "\n",
    "1. numpy是一种高效的多维数组处理第三方库，能解决大量数据运算\n",
    "2. ndarry对象->n维数组，numpy一切的基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "007ba938",
   "metadata": {},
   "source": [
    "## 如何创建n维数组（ndarry对象）\n",
    "### (array)列表转成数组（list－>numpy.ndarray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "43aad18b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5,) (2, 2) (2, 2, 1)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 根据我们列表的嵌套关系，生成对应的n维数组\n",
    "\n",
    "\n",
    "# 创建列表\n",
    "a1 = [1,2,3,4,5] # 一层嵌套\n",
    "a2 = [[1,2],[3,4]] # 二层嵌套\n",
    "a3 = [[[1],[2]],[[3],[4]]] # 三层嵌套\n",
    "\n",
    "# 一维\n",
    "array1 = np.array(a1)\n",
    "\n",
    "# 多维\n",
    "array2 = np.array(a2)\n",
    "array3 = np.array(a3)\n",
    "\n",
    "# var.shape 查看变量size\n",
    "print(array1.shape,array2.shape,array3.shape)\n",
    "\n",
    "# 讲解一下np.array中的参数\n",
    "\n",
    "### ndmin\n",
    "# 给出ndmin会限制你生成数组的最小维度\n",
    "# array = np.array(a2,ndmin=3)\n",
    "# array 返回的维度不小于三维\n",
    "\n",
    "### dtype，建议在pandas中去理解\n",
    "# 给出的dtype会使得你生成的数组中，每一个元素它的type为你设置的dtype值\n",
    "# np.dtype()能给对应列指定数据类型和列名\n",
    "### "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c2d015e",
   "metadata": {},
   "source": [
    "### (asarray)元组列表转数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "beb9f598",
   "metadata": {},
   "outputs": [],
   "source": [
    "# asarray 与arrary区别：当转化的是ndarray时，前一个是copy，后一个是重建\n",
    "x = [[1],[2]]\n",
    "a = np.asarray(x)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c11e220f",
   "metadata": {},
   "source": [
    "### （arange）指定数值范围和间隔，创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "74bc270c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 开始 结束（不包含） 步长 \n",
    "# size 刚好是 end - start 个\n",
    "start = 0\n",
    "end = 5\n",
    "step = 1\n",
    "np.arange(start,end,step)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26a41119",
   "metadata": {},
   "source": [
    "### （linspace）创建等差数列（一维）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "eba34f61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 2.5, 5. ])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num = 3\n",
    "# num 数组个数\n",
    "np.linspace(start,end,num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3e94387",
   "metadata": {},
   "source": [
    "### （logspace）创建等比数列（一维）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d895af0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.00000000e+00, 3.16227766e+02, 1.00000000e+05])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(start,end,num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44b32d1f",
   "metadata": {},
   "source": [
    "### （empty）根据参数创建随机值数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "bdb24c81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4607182418800017408, 4607182418800017408],\n",
       "       [4607182418800017408, 4607182418800017408],\n",
       "       [4607182418800017408, 4607182418800017408]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty([3,2],dtype=int)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1578af7b",
   "metadata": {},
   "source": [
    "### （zeros)创建全零数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2c4c0b74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros([2,3],dtype=float)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a4d7399",
   "metadata": {},
   "source": [
    "### (ones)创建全一数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "8a428ca6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones([3,2],dtype=float)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ec847a0",
   "metadata": {},
   "source": [
    "## ndarry对象的一些属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b9b05946",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ndarry.nidm 返回数组维度，有几根轴\n",
    "print(array3.ndim)\n",
    "\n",
    "# ndarry.shape 返回数据的具体维度，几层几列几行\n",
    "\n",
    "# ndarry.size 返回数组中元素的个数\n",
    "\n",
    "# ndarry.dtype 返回数组中元素的类型\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "780ab54c",
   "metadata": {},
   "source": [
    "## ndarray的索引与切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4efa2897",
   "metadata": {},
   "source": [
    "### 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "38d6dcfe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据：\n",
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 2  4  6]\n",
      "  [ 8 10 12]]\n",
      "\n",
      " [[ 3  6  9]\n",
      "  [12 15 18]]]\n",
      "第2块的第1行，第3列:\n",
      "6\n",
      "第1块的第2行\n",
      "[4 5 6]\n",
      "第3块：\n",
      "[[ 3  6  9]\n",
      " [12 15 18]]\n"
     ]
    }
   ],
   "source": [
    "data = [\n",
    "    [[1,2,3],\n",
    "     [4,5,6]],\n",
    "    [[2,4,6],\n",
    "     [8,10,12]],\n",
    "    [[3,6,9],\n",
    "     [12,15,18]],\n",
    "]\n",
    "# 高纬数组的索引，\n",
    "# 先从最高维度的轴索引，\n",
    "# 在依次向下维度的索引\n",
    "\n",
    "# 创建一个三维数组\n",
    "array_a = np.array(data)\n",
    "print('原始数据：')\n",
    "print(array_a)\n",
    "# 开始索引ing－－\n",
    "print('第2块的第1行，第3列:')\n",
    "print(array_a[1,0,2])\n",
    "print(\"第1块的第2行\")\n",
    "print(array_a[0,1])\n",
    "print('第3块：')\n",
    "print(array_a[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "751debca",
   "metadata": {},
   "source": [
    "### 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "25dcb7fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据：\n",
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 2  4  6]\n",
      "  [ 8 10 12]]\n",
      "\n",
      " [[ 3  6  9]\n",
      "  [12 15 18]]]\n",
      "选中第1块——第2块的所有元素\n",
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 2  4  6]\n",
      "  [ 8 10 12]]]\n",
      "选中第2块——第3块中，所有第一行的数据\n",
      "[[[2 4 6]]\n",
      "\n",
      " [[3 6 9]]]\n",
      "选中第2块——第3块中，所有第一行的数据\n",
      "[[[4]]\n",
      "\n",
      " [[6]]]\n"
     ]
    }
   ],
   "source": [
    "print('原始数据：')\n",
    "print(array_a)\n",
    "\n",
    "# 开始切片--\n",
    "print(\"选中第1块——第2块的所有元素\")\n",
    "print(array_a[0:2,])\n",
    "print(\"选中第2块——第3块中，所有第一行的数据\")\n",
    "print(array_a[1:3,0:1])\n",
    "print(\"选中第2块——第3块中，所有第一行,第二列的数据\")\n",
    "print(array_a[1:3,0:1,1:2])\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dd56d45",
   "metadata": {},
   "source": [
    "## ndarry之间的运算\n",
    "### 广播\n",
    "两个ndarry的shape属性不同时的运算规则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38e6f0a6",
   "metadata": {},
   "source": [
    "## numpy迭代数组\n",
    "np.nditer()函数可以迭代ndarry对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "a6b9ddde",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 2  4  6]\n",
      "  [ 8 10 12]]\n",
      "\n",
      " [[ 3  6  9]\n",
      "  [12 15 18]]]\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n",
      "12\n",
      "3\n",
      "6\n",
      "9\n",
      "12\n",
      "15\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "print(array_a)\n",
    "for i in np.nditer(array_a):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "467a1223",
   "metadata": {},
   "source": [
    "## 数组的一些操作\n",
    "1. 修改数组形状\n",
    "2. 翻转数组\n",
    "3. 修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb72f874",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
